﻿package com.zuzhili.database;

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ZuZhiLiDBCtrl implements InnerDBCtrl, SDCardStatusChangedCtrl/*
																		 * extends
																		 * SDSQLiteOpenHelper
																		 */{
	public static int DB_VERSION = 13;
	String mselfid;
	// byte[] mlock;

	CacheDataCtrl mcacheCtrl=new CacheDataCtrl();

	private SQLiteDatabase m_db = null;
	String memail;
	// /////////////////////////////implement
	Context mctx;

	public ZuZhiLiDBCtrl(Context ctx, String email) {
		mctx = ctx;
		memail = email;
		init(email);

		SDCardAccessor.advise(this, mctx);
	}

	public void beginTransaction() {
		if (m_db != null) {
			m_db.beginTransaction();
		} else {
		}
	}

	public void endTransaction() {
		if (m_db != null) {
			m_db.setTransactionSuccessful();
			m_db.endTransaction();
		} else {
		}
	}

	private void init(String email) {
		if (SDCardAccessor.isSDCardAvailable()) {

			if (m_db == null) {
				try {
					SDCardSqlDB db = new SDCardSqlDB(mctx, email);
					db.setDBCtrl(this);
					m_db = db.getWritableDatabase();
					mcacheCtrl.setDB(m_db);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} else {
			if (m_db == null) {
				try {
					MemeorySqlDB db = new MemeorySqlDB(mctx, email);
					db.setDBCtrl(this);
					m_db = db.getWritableDatabase();
					mcacheCtrl.setDB(m_db);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void releaseSource() {
		if (m_db != null) {

			m_db.close();
			m_db = null;
		}
		SDCardAccessor.unadvise(this);
	}

	@Override
	public void onDBCreate(SQLiteDatabase db) {
		// synchronized (mlock) {
		mcacheCtrl.onCreate(db);
	}

	@Override
	public void onDBUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		mcacheCtrl.onUpgrade(db, oldVersion, newVersion);
	}

	
	public CacheDataCtrl getCacheDataCtrl() {
		return mcacheCtrl;
	}
	

	@Override
	public void ChangedAvailable() {
		// synchronized (mlock) {
		SDCardSqlDB db = new SDCardSqlDB(mctx, memail);
		m_db = db.getWritableDatabase();
		// }
	}

	@Override
	public void ChangedUnAvailable() {
		// synchronized (mlock) {
		MemeorySqlDB db = new MemeorySqlDB(mctx, memail);
		m_db = db.getWritableDatabase();
		// }
	}

}

interface InnerDBCtrl {
	public void onDBCreate(SQLiteDatabase db);

	public void onDBUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
}

class MemeorySqlDB extends SQLiteOpenHelper {
	private InnerDBCtrl m_clDBCtrl = null;

	public static String getDataBasePath(Context context) {
		String ret = context.getDatabasePath("").getAbsolutePath();
		return ret;
	}

	public MemeorySqlDB(Context context, String email) {
		super(context, email, null, ZuZhiLiDBCtrl.DB_VERSION);
	}

	public void setDBCtrl(InnerDBCtrl clDBCtrl) {
		m_clDBCtrl = clDBCtrl;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		if (m_clDBCtrl != null) {
			m_clDBCtrl.onDBCreate(db);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (m_clDBCtrl != null) {
			m_clDBCtrl.onDBUpgrade(db, oldVersion, newVersion);
		}
	}
}

class SDCardSqlDB extends SDSQLiteOpenHelper {
	private InnerDBCtrl m_clDBCtrl;

	public SDCardSqlDB(Context context, String email) {
		super(context, SDCardAccessor.getUserDataFullPath(email), null,
				ZuZhiLiDBCtrl.DB_VERSION);
	}

	public void setDBCtrl(InnerDBCtrl clDBCtrl) {
		m_clDBCtrl = clDBCtrl;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		m_clDBCtrl.onDBCreate(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		m_clDBCtrl.onDBUpgrade(db, oldVersion, newVersion);
	}

}
